package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.IGameService;
import com.ruoyi.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IActPrizeCheckService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 获奖信息核对Controller
 * 
 * @author ruoyi
 * @date 2022-07-05
 */
@Controller
@RequestMapping("/system/check")
public class ActPrizeCheckController extends BaseController
{
    private String prefix = "system/check";

    @Autowired
    private IActPrizeCheckService actPrizeCheckService;

    @Autowired
    private IGameService gameService;

    @Autowired
    private ISysDeptService sysDeptService;

    @RequiresPermissions("system:check:view")
    @GetMapping()
    public String check()
    {
        return prefix + "/check";
    }

    /**
     * 查询获奖信息核对列表
     */
    @RequiresPermissions("system:check:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ActPrizeCheck actPrizeCheck)
    {
        SysDept dept = sysDeptService.selectDeptById(getSysUser().getDeptId());
        if(dept == null){
            return null;
        }
        actPrizeCheck.setCurUser(getSysUser().getLoginName());
        if(dept.getAncestors().startsWith("0,100") || dept.getDeptId().intValue() == 100){
            actPrizeCheck.setAllSelectPerm(true);
        }
        //查询用户所在的学院
        if(actPrizeCheck.isAllSelectPerm() == false){
            SysDept curUserFaculty = sysDeptService.getUserFaculty(getSysUser());
            if(curUserFaculty != null){
                actPrizeCheck.setCurUserFacultyId(curUserFaculty.getDeptId());
            }else{
                return null;
            }
        }
        startPage();
        List<ActPrizeCheck> list = actPrizeCheckService.selectActPrizeCheckList(actPrizeCheck);
        return getDataTable(list);
    }

    /**
     * 导出获奖信息核对列表
     */
    @RequiresPermissions("system:check:export")
    @Log(title = "获奖信息核对", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ActPrizeCheck actPrizeCheck)
    {
        SysDept dept = sysDeptService.selectDeptById(getSysUser().getDeptId());
        if(dept == null){
            return null;
        }
        actPrizeCheck.setCurUser(getSysUser().getLoginName());
        if(dept.getAncestors().startsWith("0,100") || dept.getDeptId().intValue() == 100){
            actPrizeCheck.setAllSelectPerm(true);
        }
        //查询用户所在的学院
        if(actPrizeCheck.isAllSelectPerm() == false){
            SysDept curUserFaculty = sysDeptService.getUserFaculty(getSysUser());
            if(curUserFaculty != null){
                actPrizeCheck.setCurUserFacultyId(curUserFaculty.getDeptId());
            }else{
                return null;
            }
        }
        List<ActPrizeCheck> list = actPrizeCheckService.selectActPrizeCheckList(actPrizeCheck);
        if(list != null && list.size()>0){
            for(ActPrizeCheck item : list){
                String schoolName = item.getSchool();
                String beginChar = schoolName.substring(0,1);
                if("0123456789".indexOf(beginChar) > -1){
                    String begin = schoolName.substring(0,6);
                    begin = begin.replaceAll("[0-9|-]","");
                    schoolName = begin + schoolName.substring(6);
                }
                item.setSchool(schoolName);
            }
        }
        ExcelUtil<ActPrizeCheck> util = new ExcelUtil<ActPrizeCheck>(ActPrizeCheck.class);
        return util.exportExcel(list, "获奖信息核对数据");
    }

    /**
     * 新增获奖信息核对
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 获取当前用户的学校和院系
        SysDept school = sysDeptService.getUserSchools(getSysUser());
        if(school != null){
            String schoolName = school.getDeptName();
            String beginChar = schoolName.substring(0,1);
            if("0123456789".indexOf(beginChar) > -1){
                String begin = schoolName.substring(0,6);
                begin = begin.replaceAll("[0-9|-]","");
                schoolName = begin + schoolName.substring(6);
            }
            school.setDeptName(schoolName);
        }
        mmap.put("school",school);
        SysDept faculty = sysDeptService.getUserFaculty(getSysUser());
        if(faculty != null && !faculty.getAncestors().equals("0,101") && faculty.getAncestors().startsWith("0,101")){
            mmap.put("faculty",faculty);
        }else{
            mmap.put("faculty",null);
        }
        mmap.put("curDeptId",getSysUser().getDeptId());
        mmap.put("hasPermission",sysDeptService.isUserHasPermission(getSysUser()));
        List<Game> games = gameService.selectDefault();
        mmap.put("games",games);
        return prefix + "/add";
    }

    /**
     * 新增保存获奖信息核对
     */
    @RequiresPermissions("system:check:add")
    @Log(title = "获奖信息核对", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ActPrizeCheck actPrizeCheck)
    {
        actPrizeCheck.setCreateBy(getLoginName());
        return toAjax(actPrizeCheckService.insertActPrizeCheck(actPrizeCheck));
    }

    /**
     * 修改获奖信息核对
     */
    @RequiresPermissions("system:check:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ActPrizeCheck actPrizeCheck = actPrizeCheckService.selectActPrizeCheckById(id);
        mmap.put("actPrizeCheck", actPrizeCheck);
        return prefix + "/edit";
    }

    /**
     * 修改保存获奖信息核对
     */
    @RequiresPermissions("system:check:edit")
    @Log(title = "获奖信息核对", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ActPrizeCheck actPrizeCheck)
    {
        actPrizeCheck.setUpdateBy(getLoginName());
        return toAjax(actPrizeCheckService.updateActPrizeCheck(actPrizeCheck));
    }

    /**
     * 删除获奖信息核对
     */
    @RequiresPermissions("system:check:remove")
    @Log(title = "获奖信息核对", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(actPrizeCheckService.deleteActPrizeCheckByIds(ids));
    }
}
